//
//  SYWKWebViewBridgeUtility.m
//  BasicSDKDemo
//
//  Created by huchu on 2018/8/17.
//  Copyright © 2018年 sysjones. All rights reserved.
//

#import "SYWKWebViewBridgeUtility.h"
#import "NSDictionary+YYAdd.h"
#import "AFNetworking.h"
#import "Masonry.h"
#import "ReactiveObjC.h"
#import "FMDB.h"
@implementation SYWKWebViewBridgeUtility

+ (void)evaljs : (NSDictionary *)resultDict  webView:(WKWebView *)webView {
    
    [self  evaljs:resultDict webView:webView jsMethodName:@"jsInterface"];
}


+ (void)evaljs : (NSDictionary *)resultDict  webView:(WKWebView *)webView jsMethodName:(NSString *)jsMethodName {
    NSString *messageJSON = [resultDict jsonStringEncoded];
    // 做替换的目的是为了支持， value 的值是一个   json字符串
    messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\\" withString:@"\\\\"];
    messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
    messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\'" withString:@"\\\'"];
    messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\n" withString:@"\\n"];
    messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\r" withString:@"\\r"];
    messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\f" withString:@"\\f"];
    messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\u2028" withString:@"\\u2028"];
    messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\u2029" withString:@"\\u2029"];
 
    NSString* javascriptCommand = [NSString stringWithFormat:@"%@('%@');", jsMethodName, messageJSON];
    if ([[NSThread currentThread] isMainThread]) {
        [webView evaluateJavaScript:javascriptCommand completionHandler:^(id result, NSError * _Nullable error) {
        }];
    } else {
        dispatch_sync(dispatch_get_main_queue(), ^{
            [webView  evaluateJavaScript:javascriptCommand completionHandler:^(id result, NSError * _Nullable error) {
            }];
        });
    }
}
@end
